﻿using System;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.ComponentModel.Design;
using Microsoft.Win32;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;

namespace VSShellAssist.Platform
{
    [InstalledProductRegistration(true, "#51314", "#1673", "1.0", IconResourceID = 33462, LanguageIndependentName = "VSShell Assist")]
    [ProvideProjectFactory(typeof(VSShellAssist.Platform.FlavorProjects.VSShellIsolatedApplicationSetupProjectFactory), "Visual Studio Shell Isolated Application Setup", "Visual Studio Shell Isolated Application Setup Files (*.csproj);*.csproj", null, null, null, LanguageVsTemplate = "CSharp", NewProjectRequireNewFolderVsTemplate = true)]
    [ProvideLoadKey("Standard", "1.0", "VSShellAssist", "Ktulu", 1)]
    [PackageRegistration(UseManagedResourcesOnly = true)]
    [DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\9.0")]
    [ProvideMenuResource(1000, 1)]
    [Guid(VsPackageConstants.guidVsPackagePkgString)]
    public sealed class VsPackage : Package, Microsoft.VisualStudio.Shell.Interop.IVsInstalledProduct
    {
        public VsPackage()
        {
        }

        protected override void Initialize()
        {
            base.Initialize();
            this.RegisterProjectFactory(new VSShellAssist.Platform.FlavorProjects.VSShellIsolatedApplicationSetupProjectFactory(this));
        }

        public int IdBmpSplash(out uint pIdBmp)
        {
            pIdBmp = 0;
            return Microsoft.VisualStudio.VSConstants.S_OK;
        }

        public int IdIcoLogoForAboutbox(out uint pIdIco)
        {
            pIdIco = 33462;
            return Microsoft.VisualStudio.VSConstants.S_OK;
        }

        public int OfficialName(out string pbstrName)
        {
            pbstrName = GetResourceString("51314");
            return Microsoft.VisualStudio.VSConstants.S_OK;
        }

        public int ProductDetails(out string pbstrProductDetails)
        {
            pbstrProductDetails = GetResourceString("1673");
            return Microsoft.VisualStudio.VSConstants.S_OK;
        }

        public int ProductID(out string pbstrPID)
        {
            pbstrPID = GetResourceString("17568");
            return Microsoft.VisualStudio.VSConstants.S_OK;
        }

        private string GetResourceString(string resourceName)
        {
            string resourceValue;

            Microsoft.VisualStudio.Shell.Interop.IVsResourceManager resourceManager =
                (Microsoft.VisualStudio.Shell.Interop.IVsResourceManager)GetService(typeof(Microsoft.VisualStudio.Shell.Interop.SVsResourceManager));

            if (resourceManager == null)
            {
                throw new InvalidOperationException(
                    "Could not get SVsResourceManager service. Make sure that the package is sited before calling this method");
            }

            Guid packageGuid = this.GetType().GUID;
            int hr = resourceManager.LoadResourceString(
                ref packageGuid, -1, resourceName, out resourceValue);

            Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(hr);

            return resourceValue;
        }
    }
}